package com.sense.kukoo.biz_messages.vm

import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.sense.kukoo.biz_messages.model.MessagesFollowersModel
import com.sense.kukoo.common.mvvm.model.BaseMvvmModel
import com.sense.kukoo.common.mvvm.vm.BaseViewModel
import com.sense.kukoo.common.repo.Repo
import com.sense.kukoo.common.repo.bean.ResultBean
import com.sense.kukoo.common.repo.biz.MessagesItemBean
import com.sense.kukoo.common.repo.http.KukooHttpApi
import com.sense.kukoo.common.repo.http.KukooHttpManager
import com.sense.kukoo.common.repo.http.bean.UserMessageCommentsBean
import com.sense.kukoo.common.repo.http.bean.UserMessageFollowersBean
import com.sense.kukoo.common.repo.http.bean.UserSearchBean
import com.sense.kukoo.lib_logger.LoggerManager

class MessagesFollowersViewModel: BaseViewModel() {

    private val TAG: String = javaClass.simpleName
    private var mIndex = 2
    private val mPageSize = 10
    private var mNewsNum = 0
    private var mOnlyShowNews = true
    private var mEnableLoadMore = true
    private var mNetworkRetry: ()->Unit = {}
    private val mFollowers = arrayListOf<UserMessageFollowersBean>()

    val action_back: MutableLiveData<Boolean> = MutableLiveData()
    val action_networkError: MutableLiveData<Boolean> = MutableLiveData()
    val action_finishRefresh: MutableLiveData<Boolean> = MutableLiveData()
    val data_followers:MutableLiveData<ArrayList<MessagesItemBean>> = MutableLiveData()

    override fun createModel(): BaseMvvmModel? = null

    override fun initData() {}

    fun setNewsNum(newsNum: Int){
        mNewsNum = newsNum
        if(newsNum <= 0){
            mOnlyShowNews = false
        } else if(newsNum > mPageSize){
            mOnlyShowNews = false
        }
    }

    fun onBackClick(){
        action_back.postValue(true)
    }

    private fun showFollowersData(){
        val list = arrayListOf<MessagesItemBean>()
        if (mFollowers.isEmpty()) {
            list.add(MessagesItemBean(MessagesItemBean.TYPE_EMPTY_LIST, emptyType = MessagesItemBean.TYPE_FOLLOWERS))
        } else {
            mFollowers.forEach {
                list.add(MessagesItemBean(MessagesItemBean.TYPE_FOLLOWERS, follower = it))
            }
        }
        if (mEnableLoadMore) {
            list.add(MessagesItemBean(MessagesItemBean.TYPE_VIEW_MORE))
        }
        data_followers.postValue(list)
        action_finishRefresh.postValue(true)
    }

    fun refreshFollowerData(){
        toView(ACTION_SHOW_DIALOG)
        val onError: (error: Throwable) -> Unit = {
            it.printStackTrace()
            mNetworkRetry = {refreshFollowerData()}
            action_networkError.postValue(true)
            toView(ACTION_HIDE_DIALOG)
        }
        request(onError) {
            var pageSize = mPageSize
            if (mOnlyShowNews) {
                pageSize = mNewsNum
            }
            val result = Repo.http.messageFollowers(1, pageSize)
            if (result.isSuccess()) {
                mIndex = 2
                result.data?.let {
                    mFollowers.clear()
                    mFollowers.addAll(it)
                    if (mOnlyShowNews) {
                        mEnableLoadMore = true
                    } else {
                        if(it.size < mPageSize){
                            mEnableLoadMore = false
                        } else {
                            mEnableLoadMore = true
                        }
                    }
                    showFollowersData()
                }
            } else {
                Log.e(TAG, "result=" + result.msg)
                mNetworkRetry = {refreshFollowerData()}
                action_networkError.postValue(true)
            }
            toView(ACTION_HIDE_DIALOG)
        }
    }

    fun loadMoreFollowerData(){
        if (mOnlyShowNews) {
            mOnlyShowNews = false
            refreshFollowerData()
            return
        }
        toView(ACTION_SHOW_DIALOG)
        val onError: (error: Throwable) -> Unit = {
            it.printStackTrace()
            mNetworkRetry = {loadMoreFollowerData()}
            action_networkError.postValue(true)
            toView(ACTION_HIDE_DIALOG)
        }
        request(onError) {
            val result = Repo.http.messageFollowers(mIndex, mPageSize)
            if (result.isSuccess()) {
                mIndex++
                result.data?.let {
                    mFollowers.addAll(it)
                    if(it.size < mPageSize){
                        mEnableLoadMore = false
                    }
                    showFollowersData()
                }
            } else {
                Log.e(TAG, "result=" + result.msg)
                mNetworkRetry = {loadMoreFollowerData()}
                action_networkError.postValue(true)
            }
            toView(ACTION_HIDE_DIALOG)
        }
    }

    fun deleteFollow(follow: UserMessageFollowersBean){
        request {
            val result = Repo.http.messageDelete(follow.id, 2)
            if (result.isSuccess() && true == result.data) {
                mFollowers.remove(follow)
                showFollowersData()
            } else {
                Log.e(TAG, "result=" + result.msg)
            }
            toView(ACTION_HIDE_DIALOG)
        }
    }

    fun subscription(follow: UserMessageFollowersBean){
        toView(ACTION_SHOW_DIALOG)
        request {
            val subscription = Repo.http.subscription(follow.userId)
            if (subscription.isSuccess()) {
                follow.isSub = if (follow.isFollow()) 0 else 1
                showFollowersData()
            } else {
                Log.e(TAG, "result=" + subscription.msg)
                subscription.msg.let { sendHintMessage(it) }
            }
            toView(ACTION_HIDE_DIALOG)
        }
    }

    fun onNetworkRetry(){
        mNetworkRetry()
    }

}